const httpProxy = require('http-proxy');

// 新建一个代理 Proxy Server 对象
var proxy = httpProxy.createProxyServer({});
// 捕获异常
proxy.on('error', function (err, req, res) {
    res.writeHead(500, {
        'Content-Type': 'text/plain'
    });
    res.end('Something went wrong. And we are reporting a custom error message.');
});
var server = require('http').createServer(function (req, res) {
    // 在这里可以自定义你的路由分发
    var host = req.headers.host,
        ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
    console.log("client ip:" + ip + ", host:" + host);

    switch (host) {
        //意思是监听下面的ip地址,如果匹配就转到//127.0.0.1:3000地址
        // case '192.168.1.106:4444':  //监听这个地址
        case 'http://10.60.6.11:4444/':  //监听这个地址
            proxy.web(req, res, {target: 'http://10.60.6.11:3333'}); //转到这个地址
            break;
        default:
            res.writeHead(200, {
                'Content-Type': 'text/plain'
            });
            res.end('Welcome to my server!');
    }
});

console.log("listening on port 8888");
server.listen(8888);